package com.iw.adoms.action;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iw.adoms.entity.Order;
import com.iw.adoms.entity.OrderLine;
import com.iw.adoms.service.OrderLineService;
import com.iw.adoms.service.OrderService;
import com.iw.core.orm.Filtration;
import com.iw.core.orm.Filtration.MatchType;
import com.iw.core.orm.PageData;
import com.iw.core.utils.WebUtils;

@Controller
@RequestMapping("/adoms/orderLine")
public class OrderLineAction extends BaseAction {

	private static final Logger LOGGER = Logger.getLogger(OrderLineAction.class);

	@Resource
	private OrderLineService orderLineService;
	@Resource
	private OrderService orderService;

	//
	// @Resource
	// private DepartmentService departmentService;

	@RequestMapping("/list")
	public ModelMap list(HttpServletRequest request) {
		PageData<OrderLine> pageData = new PageData<OrderLine>();
		// 给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		pageData = orderLineService.find(pageData);
		return new ModelMap(pageData);
	}

	@RequestMapping("/list/{oid}")
	public String list(HttpServletRequest request, @PathVariable("oid") Integer oid, Model model) {
		PageData<OrderLine> pageData = new PageData<OrderLine>();
		WebUtils.setPageDataParameter(request, pageData);
		pageData.getPagination().setPageSize(1000);
		Order order = orderService.find(oid);
		pageData.getFiltrations().add(new Filtration(MatchType.EQ, oid, "order.id"));
		pageData = orderLineService.find(pageData);
		model.addAttribute("pageData", pageData);
		model.addAttribute("order", order);
		return "/adoms/orderLine/list";
	}

	@RequestMapping("/listInfo/{oid}")
	public String listInfo(HttpServletRequest request, @PathVariable("oid") Integer oid, Model model) {
		try {
			List<OrderLine> orderLines = orderLineService.findList(new Filtration(MatchType.EQ, oid, "order.id"));
			model.addAttribute("orderLines", orderLines);
			Order order = orderService.find(oid);
			model.addAttribute("order", order);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/adoms/orderLine/listInfo";
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(OrderLine orderLine) {

		if (orderLine.isNew()) {
			orderLineService.save(orderLine);
		} else {
			orderLineService.update(orderLine);
		}
		return "redirect:/adoms/orderLine/list"; // 重定向
	}

	/**
	 * 专为更新orderLine商品数量用
	 * 
	 * @param orderLine
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(HttpServletRequest request,OrderLine orderLine, String flag) {
		Integer orderId = orderLine.getOrder().getId();
		try {
			orderService.updateOrderByOrderLine(this.getCurrentUser(request),orderLine, orderId);
		} catch (Exception e) {
			LOGGER.error(e);
		}
		if ("deal".equals(flag)) {
			return "redirect:/adoms/order/deal/" + orderId;// 重定向
		} else {
			return "redirect:/adoms/order/myEdit/" + orderId;// 重定向
		}
	}

	/**
	 * 将订单中的商品临时放session中
	 * 
	 * @param orderLine
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/addOlines", method = RequestMethod.POST)
	@ResponseBody
	public Integer addOlines(OrderLine orderLine, HttpServletRequest request) {
		HttpSession session = request.getSession();
		@SuppressWarnings("unchecked")
		Set<OrderLine> olines = (HashSet<OrderLine>) session.getAttribute("olines");
		if (olines == null) {
			olines = new HashSet<OrderLine>();
		}
		olines.add(orderLine);
		session.setAttribute("olines", olines);
		return orderLine.getItems();
	}

	/**
	 * 将订单中的商品临时放session中
	 * 
	 * @param orderLine
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/clearOlines")
	@ResponseBody
	public Integer clearOlines(HttpServletRequest request) {
		request.getSession().removeAttribute("olines");
		return 1;
	}

	@RequestMapping(value = "/edit/{id}")
	public String editForm(@PathVariable("id") Integer id, Model model) {
		OrderLine orderLine = orderLineService.find(id);
		model.addAttribute(orderLine);
		return "/adoms/orderLine/input";
	}

}